#import "BaseNavigationController.h"
#import "MenuViewController.h"
#import "LoginViewController.h"
#import "UserManager.h"
#import "SettingViewController.h"
#import "UserManager.h"

@implementation MenuViewController

@synthesize userStateLogin,userStateLogout,currentUserState,loginNlogoutBtn;
@synthesize currentSelect,selectJoke,selectTopic,selectJudge,selectPost;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)dealloc{
    PP_RELEASE(btnJoke);
    PP_RELEASE(textJoke);
    PP_RELEASE(iconJoke);
    PP_RELEASE(btnTopic);
    PP_RELEASE(textTopic);
    PP_RELEASE(iconTopic);
    PP_RELEASE(btnJudge);
    PP_RELEASE(textJudge);
    PP_RELEASE(iconJudge);
    PP_RELEASE(btnPost);
    PP_RELEASE(textPost);
    PP_RELEASE(iconPost);
    PP_RELEASE(userStateLogin);
    PP_RELEASE(userStateLogout);
    PP_RELEASE(currentUserState);
    PP_RELEASE(selectJoke);
    PP_RELEASE(selectTopic);
    PP_RELEASE(selectJudge);
    PP_RELEASE(selectPost);
    PP_RELEASE(currentSelect);
    PP_RELEASE(loginNlogoutBtn);
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.userStateLogin =[[LVCUserStateLogin alloc]init:self];
    self.userStateLogout =[[LVCUserStateLogout alloc]init:self];
    self.selectJoke =[[LVCSelectBase alloc] init:textJoke :iconJoke :textJoke];
    self.selectTopic =[[LVCSelectBase alloc] init:textTopic :iconTopic :textTopic];
    self.selectJudge =[[LVCSelectBase alloc] init:textJudge :iconJudge :textJudge];
    self.selectPost =[[LVCSelectBase alloc] init:textPost :iconPost :textPost];
    [self setSelect:self.selectJoke];
    
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(login)
                                                 name: @"login"
                                               object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(logout)
                                                 name: @"logout"
                                               object: nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UserManager *userManager = [UserManager sharedInstance];
    Log(@"isLogin: %@",userManager.isLogin?@"YES":@"NO");
    if (userManager.isLogin ) {
        [self setUserState:self.userStateLogin];
    }else{
        [self setUserState:self.userStateLogout];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"login" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"logout" object:nil];
    PP_RELEASE(btnJoke);
    PP_RELEASE(textJoke);
    PP_RELEASE(iconJoke);
    PP_RELEASE(btnTopic);
    PP_RELEASE(textTopic);
    PP_RELEASE(iconTopic);
    PP_RELEASE(btnJudge);
    PP_RELEASE(textJudge);
    PP_RELEASE(iconJudge);
    PP_RELEASE(btnPost);
    PP_RELEASE(textPost);
    PP_RELEASE(iconPost);
    PP_RELEASE(userStateLogin);
    PP_RELEASE(userStateLogout);
    PP_RELEASE(currentUserState);
    PP_RELEASE(selectJoke);
    PP_RELEASE(selectTopic);
    PP_RELEASE(selectJudge);
    PP_RELEASE(selectPost);
    PP_RELEASE(currentSelect);
    PP_RELEASE(loginNlogoutBtn);
    [super viewDidUnload];
}

- (void)login {
    [self setUserState:self.userStateLogin];
}

- (void)logout {
    [self setUserState:self.userStateLogout];
    if( currentSelect == self.selectJudge ){
        [self goJoke:nil];
    }
}

- (void)setUserState:(LVCUserStateBase *)newState {
    dispatch_async(dispatch_get_main_queue(), ^{
        currentUserState = newState;
        [currentUserState LVCstart];
    });
}

- (void)setSelect:(LVCSelectBase *)newSelect {
    dispatch_async(dispatch_get_main_queue(), ^{
        if( currentSelect == newSelect ){
            return;
        }
        [currentSelect LVCSstop];
        currentSelect = newSelect;
        [currentSelect LVCSstart];
    });
}

- (IBAction)goJoke:(id)sender {
    [self setSelect:self.selectJoke];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(0.2);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.viewDeckController.centerController = [[ColdAppDelegate sharedAppDelegate] jokeNavController];
            [self.viewDeckController toggleLeftView];
        });
    });
}

- (IBAction)goTopic:(id)sender {
    [self setSelect:self.selectTopic];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(0.2);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.viewDeckController.centerController = [[ColdAppDelegate sharedAppDelegate] topicNavController];
            [self.viewDeckController toggleLeftView];
        });
    });
}

- (IBAction)goJudge:(id)sender {
    if (![[UserManager sharedInstance]isLogin]) {
        iconJudge.selected = NO;
        textJudge.selected = NO;
        [self baseShowLoginSheet];
        return;
    }
    
    [self setSelect:self.selectJudge];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(0.2);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.viewDeckController.centerController = [[ColdAppDelegate sharedAppDelegate] judgeNavController];
            [self.viewDeckController toggleLeftView];
        });
    });
}

- (IBAction)goPost:(id)sender {
    [self setSelect:self.selectPost];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        sleep(0.2);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.viewDeckController.centerController = [[ColdAppDelegate sharedAppDelegate] postNavController];
            [self.viewDeckController toggleLeftView];
        });
    });
}

- (IBAction)goSetting:(id)sender {
    [[ColdAppDelegate sharedAppDelegate] settingNavController].modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self.viewDeckController presentModalViewController:[[ColdAppDelegate sharedAppDelegate] settingNavController] animated:YES];
}

- (IBAction)loginNlogout:(id)sender {
    [currentUserState LVCclick];
}
- (IBAction)touchJoke:(id)sender {
    iconJoke.selected = YES;
    textJoke.selected = YES;
}
- (IBAction)touchTopic:(id)sender {
    iconTopic.selected = YES;
    textTopic.selected = YES;
}
- (IBAction)touchJudge:(id)sender {
    iconJudge.selected = YES;
    textJudge.selected = YES;
}
- (IBAction)touchPost:(id)sender {
    iconPost.selected = YES;
    textPost.selected = YES;
}

- (IBAction)leaveJoke:(id)sender {
    iconJoke.selected = NO;
    textJoke.selected = NO;
    [currentSelect LVCSstart];
}
- (IBAction)leaveTopic:(id)sender {
    iconTopic.selected = NO;
    textTopic.selected = NO;
    [currentSelect LVCSstart];
}
- (IBAction)leaveJudge:(id)sender {
    iconJudge.selected = NO;
    textJudge.selected = NO;
    [currentSelect LVCSstart];
}
- (IBAction)leavePost:(id)sender {
    iconPost.selected = NO;
    textPost.selected = NO;
    [currentSelect LVCSstart];
}

@end

#pragma mark -------LVCSelectBase Class-------

@implementation LVCSelectBase

-(id)init:(UIButton *)pBtn :(UIButton *)pIcon :(UIButton *)pText {
    self = [super init];
    if (self) {
        btn = pBtn;
        icon = pIcon;
        text = pText;
    }
    return self;
}

-(void)LVCSstart {
    btn.selected = YES;
    text.selected = YES;
    icon.selected = YES;
}

-(void)LVCSstop {
    btn.selected = NO;
    text.selected = NO;
    icon.selected = NO;
}

@end

#pragma mark -------LVCUserStateBase Class-------
@implementation LVCUserStateBase

-(id)init:(MenuViewController *)pOwner {
    self = [super init];
    if (self) {
        owner = pOwner;
    }
    return self;
}
@end

@implementation LVCUserStateLogin
-(void)LVCstart{
    owner.loginNlogoutBtn.selected = NO;
}
-(void)LVCclick{
    [[UserManager sharedInstance] logout];
    [owner setUserState:owner.userStateLogout];
}
@end

@implementation LVCUserStateLogout
-(void)LVCstart{
    owner.loginNlogoutBtn.selected = YES;
}
-(void)LVCclick{
//    LoginViewController *LoginController = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
//    [owner.navigationController pushViewController:LoginController animated:YES];
    [owner baseLogin];
    [owner setUserState:owner.userStateLogin];
}
@end

